import 'package:flutter/material.dart';
import 'package:flutter_app/home/chip.dart';
import 'package:flutter_app/home/custompaint.dart';
import 'package:flutter_app/home/expansion_panel_list.dart';
import 'package:flutter_app/home/hero.dart';
import 'package:flutter_app/home/network.dart';
import 'package:flutter_app/home/platform_channel.dart';
import 'package:flutter_app/home/refresh.dart';
import 'package:flutter_app/home/sliver.dart';
import 'package:flutter_app/home/sp.dart';
import 'package:flutter_app/home/sqlite.dart';
import 'package:flutter_app/home/tabbar.dart';
import 'package:flutter_app/home/tween.dart';
import 'package:flutter_app/home/wrap.dart';
import 'package:flutter_app/model/user.dart';
import 'package:flutter_app/model/user_redux.dart';
import 'package:flutter_app/utils/channel.dart';
import 'package:flutter_redux/flutter_redux.dart';

class MainPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _MainPage();
  }
}

class _MainPage extends State<MainPage> with AutomaticKeepAliveClientMixin {
  DateTime _lastPressedAt; //上次点击时间
  @override
  Widget build(BuildContext context) {
    return WillPopScope(
        child: Scaffold(
          body: ListView(
            children: <Widget>[
              StoreConnector<UserState, User>(
                converter: (store) => store.state.user,
                builder: (context, model) {
                  return Center(
                    child: Text(
                      model?.accountName ?? "未登录",
                      style: Theme.of(context).textTheme.display1,
                    ),
                  );
                },
              ),
              item("shared_preferences", Icons.date_range, SpPage()),
              item("Sqlite", Icons.dashboard, SqlitePage()),
              item("NetWork", Icons.network_wifi, NetWorkPage()),
              item("platform channel", Icons.scanner, PlatformChannel()),
              item("TabBar", Icons.tab, TabBarPage()),
              item("Refresh", Icons.refresh, RefreshPage()),
              item("Tween", Icons.accessibility, TweenPage()),
              item("Hero", Icons.people, HeroPage()),
              item("SliverAppBar", Icons.screen_lock_portrait,
                  SliverAppBarPage()),
              item("Wrap", Icons.wrap_text, WrapPage()),
              item("Chip", Icons.child_care, ChipPage()),
              item("ExpansionPanelList", Icons.expand_more,
                  ExpansionPanelListPage()),
              item("CustomPainter", Icons.pie_chart, CustomPaintPage()),
            ],
          ),
        ),
        onWillPop: () async {
          if (_lastPressedAt == null ||
              DateTime.now().difference(_lastPressedAt) >
                  Duration(seconds: 2)) {
            //两次点击间隔超过2秒则重新计时
            _lastPressedAt = DateTime.now();
            Channel.showToast("再按一次退出应用程序");
            return false;
          }
          return true;
        });
  }

  Widget item(String title, IconData iconData, Widget widget) {
    return Column(
      children: <Widget>[
        Container(
          child: ListTile(
            leading: Icon(iconData),
            title: Text(title),
            onTap: () {
              Navigator.push(
                  context, MaterialPageRoute(builder: (context) => widget));
            },
          ),
          color: Colors.white,
        ),
        Divider(height: 1),
      ],
    );
  }

  //保留tab状态
  @override
  bool get wantKeepAlive => true;
}
